//index.js
var t = getApp(),
  a = t.requirejs("core"),
  c = t.requirejs("api/index"),
  e = (t.requirejs("icons"), t.requirejs("wxParse/wxParse"));//对图片进行处理
Page({
  data: {
    route: "power",
    menu: t.tabBar,
    menu_show:false,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    circular: true,
    icons: t.requirejs("icons"),
    show:false,
    display: true,
    hotimg: "/yb_shop/static/images/hotdot.jpg",
    notification: "/yb_shop/static/images/notification.png",
    default_pic: '/yb_shop/static/images/add_pic.jpg',
    background: t.config.background,
    showtabbar: false,
    markers: [{
      iconPath: "/yb_shop/static/images/red_position_icon.png",
      title:'地理位置',
      latitude: 34.6284500000,
      longitude: 112.4282100000,
      width: 50,
      height: 50
    }],
    video: false,
    config: t.config,
    id:"0",
    bookData:{}//清空表单数据用
  },
  //底部导航跳转
  menu_url: function (k) {
    a.menu_url(k, 2);
  },

  //跳转+收集表单id
  formSubmit: function (k) {

    var vv = { currentTarget: { dataset: {} } },
      data = k.detail.value;
    vv.currentTarget.dataset = data;
    a.menu_url(vv, 1);

    var d = {}
    d.formid = k.detail.formId;
    d.openid = getApp().getCache("userinfo").openid;
    d.username = getApp().getCache("userinfo").nickName;
    a.get('Market/getFormid', d, function (t) {
      console.log(t)
    })

  },

  //跳转
  to_url: function (k) {
    a.menu_url(k, 1);
  },

  onLoad: function (options) {
    console.log(options);
    var that=this;
    if (options != null && options != undefined)
    {
      that.setData({ 
        id: options.id, 
        tabbar_index: options.tabbar_index ? options.tabbar_index : -1
      });
    }

    getApp().get_menu(function (x) {
      that.setData({
        menu: getApp().tabBar
      })
    
      if (that.data.tabbar_index >= 0)
      {
        that.setData({
          showtabbar: getApp().tabBar.IsDiDis ? getApp().tabBar.IsDiDis : false,
        })
      }
      that.getList();
      a.setting();
    })
    
    
   
   
  },
  formBook:function(e){
    var that=this,
    data=e.detail.value,
    uid = getApp().getCache("userinfo").uid;
    if (data.name==''){
      a.error('姓名不能为空');
      return;
    } else if (uid==null) {
      a.error('请先登录');
      setTimeout(function(){
        a.jump('/yb_shop/pages/index/index',2);
      },1000)	
      return;
    }else if(data.phone.length!=11){
      a.error('手机号格式不正确');
      return;
    }
    data.user_id = uid;
    a.get('Index/WriteBook',data,function(t){
      if(t.code==0){
        that.setData({
          bookData:{}
        });
        a.success('提交成功');
      }else{
        a.alert(t.msg);
      }
    })
   
  },

  onShow: function () {

  },
 getList:function(){
   var that = this,
   id=that.data.id;

   c.indexMod(that, function (t) {
     console.log(t);
             that.setData({
               index:t.index,
               show:t.show,
               page:t.page,
             });

             wx.setNavigationBarColor({
               frontColor: t.page.text_color,
               backgroundColor: t.page.nv_color,
               animation: 
               {
                 duration: 0,
                 timingFunc: "easeIn"
               }
              });

             if (wx.setBackgroundColor) {
               wx.setBackgroundColor({
                 backgroundColor: t.page.bg_color ? t.page.bg_color : '#f2f3f4'
               })
             }

   }, 'power', id)
    },
  /**
 * 下拉刷新
 */
  onPullDownRefresh: function () {
    this.onLoad()
    wx.stopPullDownRefresh();
  },
  /**
   * 地图
   */
  navigate: function (e) {
    var t = a.pdata(e);
    if (t.name && t.lat && t.lng) {
      a.tx_map(t.lat, t.lng, t.name)
    } else {
      a.toast('不能获取到该位置')
    }
  },
  phone: function (t) {
    a.phone(t)
  },
  // 万能表单提交
  formPower: function (e) {
    var form_id = e.detail.formId;
    c.formSubmit(this, e, form_id);
  },

  bindPickerChange: function (e) {
    c.bindPickerChange(this, e);
  },
  listen_time_two: function (e) {
    c.listen_time_two(this, e);
  },
  chooseImageTap1: function (e) {
    var id = a.pdata(e).id;
    c.upload(this, id, 'form_pic', 1)
  },
  chooseImageTap2: function (e) {
    var id = a.pdata(e).id;
    c.upload(this, id, 'form_pic', 0)
  },
  Image_del: function (e) {
    c.Image_del(this, e);
  },
  /**
   * 图片预览
   */
  previewImage:function(e){
   var url=a.pdata(e).url,
   arr=a.pdata(e).arr;
   a.previewImage(url, arr,'imgurl')
  },
  /**
  * 监听开始时间
  */
  listenerTime: function (e) {
    //调用setData()重新绘制
    this.setData({
      "bookData.book_time": e.detail.value,
    });
  },
  //分享
  onShareAppMessage: function () {
    return a.onShareAppMessage();
  }
})
